<?php
function make_clickable($text)
{
	$replaced = array();
    $ret = ' ' . $text;
	if(preg_match_all("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise",$ret,$matches,PREG_SET_ORDER)) {
		foreach($matches as $match) {
			if(!in_array($match[0],$replaced)) {
				if(substr($match[2],-1) == '.') {
					$match[3] = '.';
					$match[2] = substr($match[2],0,(strlen($match[2])-1));
				}
				else {
					$match[3] = '';
				}
				$ret = str_replace($match[0],$match[1].'<a href="'.$match[2].'">'.$match[2].'</a>'.$match[3],$ret);
				$replaced[] = $match[0];
			}
		}
	}
	if(preg_match_all("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise",$ret,$matches,PREG_SET_ORDER)) {
		foreach($matches as $match) {
			if(!in_array($match[0],$replaced)) {
				if(substr($match[2],-1) == '.') {
					$match[3] = '.';
					$match[2] = substr($match[2],0,(strlen($match[2])-1));
				}
				else {
					$match[3] = '';
				}
				$ret = str_replace($match[0],$match[1].'<a href="'.$match[2].'">'.$match[2].'</a>'.$match[3],$ret);
				$replaced[] = $match[0];
			}
		}
	}
#	$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret);
#	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $ret);
#    $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
    $ret = substr($ret, 1);
    return($ret);
}

$where = array();

if(isset($_GET['sis-simple-search'])) {
	foreach(array('SUBJECT','COURSE_TITLE_LONG','LOCATION','INSTRUCTORNAME','NOTETEXT') as $pr) {
		$where[] = "$pr LIKE '%".str_replace("'","''",$_GET['sis-simple-search'])."%'";
	}

	if(count($where)) {
		$simpleclause = " WHERE (".implode(' OR ',$where).")";
		$where = array();
	}
	
	$simpleclause .= ' AND '."SDATE>='".date("Y-m-d",strtotime(date("Y-m-d",time())." -5 days"))."'";

}
if(isset($_GET['id']) && in_array(strtoupper($_GET['id']),$locations)) {
	unset($_GET['LOCATION']);
	$where[] = "LOCATION='".str_replace("'","''",$_GET['id'])."'";
	$locset=true;
}
elseif(isset($_GET['id']) && array_key_exists($_GET['id'],$terms)) {
	unset($_GET['STRM']);
	$where[] = "STRM=".str_replace("'","''",$_GET['id']);
}
elseif(!isset($_GET['STRM'])) {
	$where[] = "STRM>=".CURTERM." AND STRM <=".MAXTERM;
}
$where[] = "SDATE>='".date("Y-m-d",strtotime(date("Y-m-d",time())." -5 days"))."'";

$props = array('subject','catalog_nbr','credits','location','session_type','ses','strm');

foreach($props as $p) {
	$p = strtoupper($p);
	if(isset($_GET[$p])) {
		if(!is_numeric($_GET[$p]) && !is_array($_GET[$p])) {
			$_GET[$p] = "'".str_replace("'","''",$_GET[$p])."'";
			$where[] = "$p=".$_GET[$p];
		}
		elseif(is_array($_GET[$p])) {
			$iw = array();
			foreach($_GET[$p] as $pi) {
				if(!is_numeric($pi)) {
					$pi = "'".str_replace("'","''",$pi)."'";
				}
				$iw[] = "$p=$pi";
			}
			$where[] = "(".implode(' OR ',$iw).")";
		}
		elseif(is_numeric($p)) {
			$where[] = "$p=".$_GET[$p];
		}
		if($p == 'LOCATION') {
			$locset = true;
		}
	}
}
unset($p);

$searchprops = array('instructorname','course_title_long','stnd_mtg_pat','lue');
foreach($searchprops as $p) {
	$iw = array();
	$p = strtoupper($p);
	if(isset($_GET[$p]) && !empty($_GET[$p])) {
		if(is_array($_GET[$p]) && !empty($_GET[$p])) {
			foreach($_GET[$p] as $t) {
				if($p == 'LUE' && $t == 'VIRT') {
					$iw[] = "CLASS_SECTION LIKE '%W%'";
					$iw[] = "STND_MTG_PAT='VIRT'";
				}
				else {
					if(!is_numeric($t)) {
						$t = "'%".str_replace("'","''",$t)."%'";
					}
					$iw[] = "$p LIKE $t";
				}
			}
			if(count($iw)) {
				$where[] = "(".implode(' OR ',$iw).")";
			}
		}
		else {
			foreach(explode(' ',$_GET[$p]) as $t) {
				$where[] = "$p LIKE '%".str_replace("'","''",$t)."%'";
			}
		}
	}
}
if(isset($_GET['STIME']) && is_array($_GET['STIME'])) {
	$iw = array();
	foreach($_GET['STIME'] as $t) {
		if($t == 12) { $iw[] = "STIME <= '12'"; }
		elseif($t==16) { $iw[] = "STIME >='12' AND STIME <='16'"; }
		elseif($t==23) { $iw[] = "STIME >='16'"; }
	}
	if(count($iw)) { $where[] = "(".implode(' OR ',$iw).")"; }
}

if(isset($_GET['opencourses']) && $_GET['opencourses'] == 'true') {
	$where[] = "ENRL_TOT<ENRL_CAP";
}

if(isset($_GET['id']) && !isset($_GET['STRM']) && !array_key_exists($_GET['id'],$terms)) {
	$where[] = 'STRM>='.CURTERM." AND STRM <=".MAXTERM;
}

if(count($where)) {
	$whereclause = " WHERE ".implode(' AND ',$where);
}

$whereclause = (isset($whereclause) && isset($simpleclause) && !empty($whereclause) && !empty($simpleclause)) ? $whereclause.' AND ('.str_replace('WHERE','',$simpleclause).')' : ((isset($whereclause) && !empty($whereclause)) ? $whereclause : $simpleclause);

if(isset($_GET['orderby'])) {
	$orderby = ' ORDER BY STRM ASC, locationorder ASC,'.lfescape($_GET['orderby']).' ASC';
}
elseif(isset($_GET['orderbysubject'])) {
	$orderby = ' ORDER BY STRM ASC, SUBJECT ASC, CATALOG_NBR ASC, locationorder ASC,'.lfescape($_GET['orderby']).' ASC';
}
else {
	$orderby = ' ORDER BY STRM ASC, locationorder ASC, SUBJECT ASC, CATALOG_NBR ASC, CLASS_NBR ASC, INSTRUCTORNAME ASC, STND_MTG_PAT ASC, STIME ASC';
}

if($tbls == 0) {
	require_once($GLOBALS['paths']['includepath'].'current-students/class-schedule-sis/ms-lookup-query.php');
}
else {
	require_once($GLOBALS['paths']['includepath'].'current-students/class-schedule-sis/my-lookup-query.php');
}

if($total <= 0) {
	echo '<div id="noresults" class="valid-message" style="margin: 2em;">No courses match the criteria you specified.</div>';
	/*echo '<div style="height: 0; width: 0; margin: 0; padding: 0; border: none; line-height: 0; overflow: hidden;">'.$topclause.'</div>';*/
}

?>
